// utils/request.js  
const BASE_URL = 'http://api.xjznpt.cn/api'

export const http = (options) => {
	return new Promise((resolve, reject) => {
		if (options.url.indexOf("http") === -1) {
			options.url = BASE_URL + options.url
		}
		uni.request({
			url: options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: {
				'Content-Type': options.contentType || 'application/json',
				'Authorization': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.error_description || '请求异常，请稍后重试',
						icon: 'none'
					});
					reject(res)
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '网络连接失败',
					icon: 'none'
				})
				reject(err)
			}
		})
	})
}